package com.djsearch.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.djsearch.R;
import com.djsearch.bean.WineShop;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;

/**
 * 主页进入酒店页面（即为查找酒店）
 * @author chengyang2
 *
 */
public class SearchJiuDianActivity extends BaseActivity{

	private Context mContext;
	private Button backBtn,searchBtn;
	private RelativeLayout stayCityLayout,stayTimeLayout,stayDaysLayout;
	private TextView stayCityText;
	private List<WineShop> houseList;
	private EditText editJiuDianName;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search_jiudian_layout);
		mContext = this;
		houseList = new ArrayList<WineShop>();
		//初始化主界面
		initView();
		//初始化主界面相关点击事件
		initOnClick();
		//
		initHandler();
	}
	
	@Override
	void initHandler() {
		// TODO Auto-generated method stub
		
	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		backBtn = (Button)findViewById(R.id.btnBack);
		searchBtn = (Button)findViewById(R.id.searchBtn);
		stayCityLayout = (RelativeLayout)findViewById(R.id.stayCityLayout);
		stayTimeLayout = (RelativeLayout)findViewById(R.id.stayTimeLayout);
		stayDaysLayout = (RelativeLayout)findViewById(R.id.stayDaysLayout);
		stayCityText = (TextView)findViewById(R.id.stayCityText);
		editJiuDianName = (EditText)findViewById(R.id.editName);
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		
		
        backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(mContext, MainActivity.class, 2);
				
				
			}
		});
        searchBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				startActivity(mContext, JiuDianActivity.class, 0);
				if (stayCityText.getText().toString().equals("")) {
					startAnimation(stayCityLayout);
				} else {
					requestData();
				}
				
			}
		});
        stayCityLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();   
				intent.setClass(getBaseContext(), ChooseCityActivity.class);   
				startActivityForResult(intent, 1);
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				
			}
		});
        
        stayDaysLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editJiuDianName.setVisibility(View.VISIBLE);
				editJiuDianName.startAnimation(AnimationUtils.loadAnimation(SearchJiuDianActivity.this, R.anim.push_left_in));
			}
		});
	}

	
	@Override   
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
	       // TODO Auto-generated method stub    
	       Bundle bundle;   
	       switch (requestCode) {   
	       case 1:   
	           if (resultCode == RESULT_OK){   
	               bundle = data.getExtras();   
	               String beginName = bundle.getString("NAME"); 
	               stayCityText.setText(beginName);
	           }  
	           break;
	       case 2:  
	    	   if (resultCode == RESULT_OK){   
	    		   bundle = data.getExtras();   
	               String nameString = bundle.getString("NAME");  
//	               arriveCityText.setText(nameString);
	           } 
	           break;   
	       default:   
	           break;   
	       }   
	    } 
	
	
	
	/**
	 * 开始查询，请求数据
	 */
	private void requestData() {
		IRequest request = new JSONReq("house");
		request.putParam("queryByName", editJiuDianName.getText().toString());
		request.putParam("city", stayCityText.getText().toString());
		request.putParam("page", "1");
		request.putParam("pageNum", "3");
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onStart() {
				showDialog("正在加载...");
			}
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
					getDataToResolve(response);
				    Toast.makeText(mContext, "成功", 5000).show();
				} else if (IResponse.Fail==result) {
					Toast.makeText(mContext, "失败", 5000).show();
				}
					missDialog();
			}
			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				missDialog();
			}
			
		});
	
		
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		houseList.clear();
		super.onResume();
	}
	
	/**
	 * 点击返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		startActivity(mContext, MainActivity.class,2);
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * {"housePlace":"中山北路3612号","houseName":"99旅馆连锁（上海华师大店）",
	 * "bigImage":"http:\/\/Images4.c-ctrip.com\/target\/hotel\/139000\/138403\/FDE5057496E047598FABB1293DC8DBDA_100_75.Jpg",
	 * "smallImage":"http:\/\/Images4.c-ctrip.com\/target\/hotel\/139000\/138403\/FDE5057496E047598FABB1293DC8DBDA_100_75.Jpg"},
	 */
	
	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub
		try {
			JSONArray jsonArray = response.getJSONArray("returnMessage");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obJsonObject = jsonArray.getJSONObject(i);
				WineShop wineShop = new WineShop();
				wineShop.setHouseName(obJsonObject.getString("houseName"));
				wineShop.setHouseAddress(obJsonObject.getString("housePlace"));
				wineShop.setSmallImage(obJsonObject.getString("smallImage"));
				houseList.add(wineShop);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (houseList.size() != 0) {
				Intent intent=new Intent(mContext,JiuDianActivity.class);
				intent.putExtra("data", houseList.toArray());
				Bundle localBundle = new Bundle();
				intent.putExtras(localBundle);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			} else {
				Toast.makeText(mContext, "没有数据", 5000).show();
			}
		}
		
	}
}
